package arr

/*
时间复杂度:O(CN)
空间复杂度:O(N)
bug free: 不要忘记else的情况，否则会无限循环
*/
func transformArray(arr []int) []int {
	if len(arr) <= 2 {
		return arr
	}
	var result []int
	for {
		result = make([]int, len(arr))
		result[0], result[len(arr)-1] = arr[0], arr[len(arr)-1]
		change := false
		for i := 1; i < len(arr)-1; i++ {
			if arr[i] < arr[i-1] && arr[i] < arr[i+1] {
				result[i] = arr[i] + 1
				change = true
			} else if arr[i] > arr[i-1] && arr[i] > arr[i+1] {
				result[i] = arr[i] - 1
				change = true
			} else {
				result[i] = arr[i]
			}
		}
		arr = result
		if !change {
			break
		}
	}
	return arr
}
